package com.gjp.controller;

import com.gjp.bean.User;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * Created by 郭金鹏 on 2018/7/15.
 */
@RestController
public class IndexController {

    @GetMapping("/")
    public ModelAndView index(User user){
        ModelAndView modelAndView = new ModelAndView("index");
        return modelAndView;
    }

    @PostMapping("/")
    public ModelAndView check(@Valid User user,BindingResult bindingResult){
        ModelAndView modelAndView = new ModelAndView("index");

        //如果验证通过，则重定向
        if (!bindingResult.hasErrors()){
            modelAndView.setViewName("redirect:/results");
        }

        return modelAndView;
    }

    @GetMapping("/results")
    public ModelAndView results(){
        ModelAndView modelAndView = new ModelAndView("results");
        return modelAndView;
    }
}
